package com.ibeeking.found.cop.b.rest.service.impl;

import com.ibeeking.found.common.config.prop.FileOssProp;
import com.ibeeking.found.common.utils.UrlUtils;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.cop.b.rest.service.ICopSourceService;
import com.ibeeking.found.cop.service.common.bo.CopSourceBO;
import com.ibeeking.found.cop.service.common.constants.SourceConstant;
import com.ibeeking.found.cop.service.common.dos.CopSourceDO;
import com.ibeeking.found.cop.service.common.param.CopSourceAddParam;
import com.ibeeking.found.cop.service.common.param.CopSourceModifyBatch;
import com.ibeeking.found.cop.service.common.param.CopSourceParam;
import com.ibeeking.found.cop.service.common.param.SourceParam;
import com.ibeeking.found.cop.service.common.query.CopSourcePageQuery;
import com.ibeeking.found.cop.service.common.query.CopSourceQuery;
import com.ibeeking.found.cop.service.common.vo.CopSourceVO;
import com.ibeeking.found.cop.service.common.vo.CopSourceViewVo;
import com.ibeeking.found.cop.service.mapper.CopSourceMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName CopSourceServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-02-05 16:17
 **/
@Service
public class CopSourceServiceImpl extends ServiceImpl<CopSourceMapper, CopSourceDO> implements ICopSourceService {

    @Resource
    private CopSourceMapper copSourceMapper;
    @Resource
    private FileOssProp fileOssProp;

    @Override
    public List<CopSourceVO> list(CopSourceQuery query) {
        LambdaQueryWrapper<CopSourceDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SourceConstant.MODIFY_TIME.equals(query.getRule()), CopSourceDO::getModifyTime)
                .orderByDesc(SourceConstant.CREATE_TIME.equals(query.getRule()), CopSourceDO::getCreateTime)
                .orderByAsc(SourceConstant.NAME.equals(query.getRule()), CopSourceDO::getName)
                .eq(CopSourceDO::getType, query.getType())
                .eq(query.getParentId() != null, CopSourceDO::getParentId, query.getParentId())
                .like(StringUtils.isNotEmpty(query.getName()), CopSourceDO::getName, query.getName())
                .eq(StringUtils.isNotEmpty(query.getContentType()), CopSourceDO::getContentType, query.getContentType());
        List<CopSourceDO> copSourceDOS = copSourceMapper.selectList(queryWrapper);
        for (CopSourceDO copSourceDO : copSourceDOS) {
            if (StringUtils.isNotEmpty(copSourceDO.getPath())) {
                copSourceDO.setPath(UrlUtils.addUrlDomainName(fileOssProp.getHost(), copSourceDO.getPath()));
            }
        }
        return JsonUtils.jsonToList(copSourceDOS, CopSourceVO.class);
    }

    @Override
    public Page<CopSourceVO> pageList(CopSourcePageQuery pageQuery) {
        LambdaQueryWrapper<CopSourceDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(pageQuery.getShopId() != null, CopSourceDO::getShopId, pageQuery.getShopId())
                .eq(pageQuery.getParentId() != null, CopSourceDO::getParentId, pageQuery.getParentId())
                .eq(CopSourceDO::getType, pageQuery.getType())
                .eq(CopSourceDO::getMerchantsBusiness, pageQuery.getMerchantsBusiness())
                .eq(StringUtils.isNotEmpty(pageQuery.getContentType()), CopSourceDO::getContentType, pageQuery.getContentType())
                .orderByDesc(SourceConstant.MODIFY_TIME.equals(pageQuery.getRule()), CopSourceDO::getModifyTime)
                .orderByDesc(SourceConstant.CREATE_TIME.equals(pageQuery.getRule()), CopSourceDO::getCreateTime)
                .orderByAsc(SourceConstant.NAME.equals(pageQuery.getRule()), CopSourceDO::getName)
                .like(StringUtils.isNotEmpty(pageQuery.getName()), CopSourceDO::getName, pageQuery.getName())
                .like(StringUtils.isNotEmpty(pageQuery.getRemark()), CopSourceDO::getRemark, pageQuery.getRemark());
        Page page = new Page(pageQuery.getPageNum(), pageQuery.getPageSize());
        Page<CopSourceDO> pages = copSourceMapper.selectPage(page, queryWrapper);
        Page<CopSourceVO> goodsSourceVOPage = BeanUtil.convertPage(pages, CopSourceVO.class);
        Integer count = copSourceMapper.selectCount(queryWrapper);
        for (CopSourceVO copSourceVO : goodsSourceVOPage.getRecords()) {
            if (StringUtils.isNotEmpty(copSourceVO.getPath())) {
                copSourceVO.setPath(UrlUtils.addUrlDomainName(fileOssProp.getHost(), copSourceVO.getPath()));
            }
            copSourceVO.setAllCount(count);
        }
        return goodsSourceVOPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(CopSourceAddParam param) {
        for (SourceParam sourceParam : param.getSources()) {
            CopSourceDO copSourceDO = BeanUtil.convertBean(param, CopSourceDO.class);
            copSourceDO.setCreateBy(UserUtils.getUserId());
            copSourceDO.setCreateTime(LocalDateTime.now());
            copSourceDO.setModifyTime(LocalDateTime.now());
            copSourceDO.setPath(sourceParam.getPath());
            copSourceDO.setName(sourceParam.getName());
            copSourceDO.setRemark(sourceParam.getRemark());
            //parentId为1 为 未分组
            if (copSourceDO.getParentId() == 1) {
                copSourceDO.setParentId(SourceConstant.parentId);
            }
            copSourceMapper.insert(copSourceDO);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(CopSourceParam param) {
        LambdaQueryWrapper<CopSourceDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CopSourceDO::getName, param.getName());
        List<CopSourceDO> copSourceDOS = copSourceMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(copSourceDOS)) {
            throw new BusinessException("素材名称不能重复");
        }
        CopSourceDO copSourceDO = BeanUtil.convertBean(param, CopSourceDO.class);
        copSourceDO.setPath(UrlUtils.removeMallUrlDomainName(fileOssProp.getHost(), copSourceDO.getPath()));
        copSourceDO.setModifyBy(UserUtils.getUserId());
        copSourceDO.setModifyTime(LocalDateTime.now());
        return copSourceMapper.updateById(copSourceDO) > 0;
    }

    @Override
    public CopSourceViewVo view(Long id) {
        CopSourceDO copSourceDO = copSourceMapper.selectById(id);
        copSourceDO.setPath(UrlUtils.addUrlDomainName(fileOssProp.getHost(), copSourceDO.getPath()));
        ArrayList<String> list = new ArrayList<>();
        CopSourceViewVo copSourceViewVo = BeanUtil.convertBean(copSourceDO, CopSourceViewVo.class);
        if (copSourceDO.getParentId() == 0) {
            list.add(id + "");
        } else {
            CopSourceDO copSourceDO1 = copSourceMapper.selectById(copSourceDO.getParentId());
            if (copSourceDO1 != null) {
                list.add(copSourceDO1.getParentId() + "");
            }
            list.add(copSourceDO.getParentId() + "");
        }
        copSourceViewVo.setParentIds(list);
        return copSourceViewVo;
    }


    @Override
    public Boolean del(Long id) {
        return copSourceMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delBatchIds(List<Long> ids) {
        Boolean flag = false;
        List<List<Long>> parts = Lists.partition(ids, 1000);
        for (List<Long> part : parts) {
            flag = copSourceMapper.deleteBatchIds(part) > 0;
        }
        return flag;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modifyBatchIds(CopSourceModifyBatch param) {
        //parentID为1移动到未分组
        if (param.getParentId() == 1) {
            param.setParentId(SourceConstant.parentId);
        }
        LambdaQueryWrapper<CopSourceDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(CopSourceDO::getId, param.getIds());
        CopSourceDO copSourceDO = new CopSourceDO();
        copSourceDO.setParentId(param.getParentId());
        copSourceDO.setModifyTime(LocalDateTime.now());
        return copSourceMapper.update(copSourceDO, queryWrapper) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyParentId(Long parentId) {
        LambdaQueryWrapper<CopSourceDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CopSourceDO::getParentId, parentId);
        List<CopSourceDO> copSourceDOS = copSourceMapper.selectList(queryWrapper);
        for (CopSourceDO copSourceDO : copSourceDOS) {
            copSourceDO.setParentId(SourceConstant.parentId);
            copSourceDO.setModifyTime(LocalDateTime.now());
            copSourceMapper.updateById(copSourceDO);
        }
    }

    @Override
    public Integer listCountByParentIdAndType(CopSourceBO param) {
        LambdaQueryWrapper<CopSourceDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(param.getShopId() != null, CopSourceDO::getShopId, param.getShopId())
                .eq(CopSourceDO::getMerchantsBusiness, param.getMerchantsBusiness())
                .eq(CopSourceDO::getParentId, param.getId())
                .eq(CopSourceDO::getType, param.getType());
        return copSourceMapper.selectCount(queryWrapper);
    }
}